<#setting number_format="#">
<#macro buildFieldName field withoutPrefix><#if withoutPrefix || (field.prefix!"")?length == 0><#if (field.value!"")?contains(".")>${field.value!""}<#else>"${field.value!""}"</#if><#else>"${field.prefix!""}", <#if (field.value!"")?contains(".")>${field.value!""}<#else>"${field.value!""}"</#if></#if></#macro>
<#macro toSetIdValue><#if primaryKey?? && !primaryKey.autoIncrement>.${primaryKey.name!"id"}(id)<#elseif multiPrimaryKey>.id(id)</#if></#macro>
<#macro toSetFieldValue field><#if field.type?ends_with("String")>"${field.demoValue}"<#elseif field.type?ends_with("Long")><#if ((field.demoValue!"")?trim)?length == 0>0L<#else>${field.demoValue?trim}L</#if><#elseif field.type?ends_with("Integer")><#if ((field.demoValue!"")?trim)?length == 0>0<#else>${field.demoValue?trim}</#if><#elseif field.type?ends_with("Double")><#if ((field.demoValue!"")?trim)?length == 0>0d<#else>${field.demoValue?trim}d</#if><#elseif field.type?ends_with("Float")><#if ((field.demoValue!"")?trim)?length == 0>0f<#else>${field.demoValue?trim}f</#if><#elseif field.type?ends_with("Boolean")><#if ((field.demoValue!"")?trim)?length == 0>false<#else>${field.demoValue?trim}</#if></#if></#macro>
/*
 * Copyright ${.now?string("yyyy")} the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ${app.packageName}.repository.impl;

<#if entityPackageName??>import ${entityPackageName}.*;<#elseif api.entityClass??>import ${api.entityClass};</#if>
import ${app.packageName}.bean.*;
import ${app.packageName}.repository.I${api.name?cap_first}Repository;
import ${app.packageName}.vo.${api.name?cap_first}VO;
import net.ymate.platform.core.persistence.Fields;
import net.ymate.platform.core.persistence.IResultSet;
import net.ymate.platform.core.persistence.Page;
import net.ymate.platform.core.persistence.Params;
import net.ymate.platform.persistence.jdbc.JDBC;
import net.ymate.platform.persistence.jdbc.query.Cond;
import net.ymate.platform.persistence.jdbc.query.OrderBy;
import net.ymate.platform.core.annotation.EnableAutoScan;
import net.ymate.platform.core.annotation.EnableBeanProxy;
import net.ymate.platform.core.annotation.EnableDevMode;
import net.ymate.platform.core.beans.annotation.Inject;
import net.ymate.platform.test.YMPJUnit4ClassRunner;
import org.apache.commons.lang3.ArrayUtils;
import org.junit.*;
import org.junit.runner.RunWith;

/**
 * <#if api.description?? && (api.description?length > 0)>${api.description}<br></#if>
 *
 * ${api.name?cap_first}RepositoryTest generated By CrudMojo on ${.now?string("yyyy/MM/dd a HH:mm")}
 *
 * @author ${app.author!"YMP (https://www.ymate.net/)"}
 * @version ${app.version!"1.0.0"}
 */
@RunWith(YMPJUnit4ClassRunner.class)
@EnableAutoScan
@EnableBeanProxy
@EnableDevMode
public class ${api.name?cap_first}RepositoryTest {

    @Inject
    private JDBC database;

    @Inject
    private I${api.name?cap_first}Repository repository;

    @BeforeClass
    public static void setUpClass() {
    }

    @AfterClass
    public static void tearDownClass() {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    <#if !api.view>@Test
    public void testBuildPrimaryKey() {<#if multiPrimaryKey><#list nonAutoPrimaryFields as p>
        ${p.type} ${p.name} = null;</#list>
        ${api.name?cap_first}PK primaryKey = ${api.name?cap_first}Repository.buildPrimaryKey(<#list nonAutoPrimaryFields as p>${p.name}<#if p_has_next>, </#if></#list>);
        Assert.assertNotNull(primaryKey);<#elseif primaryKey?? && !primaryKey.autoIncrement>
        ${primaryKey.type} primaryKey = ${api.name?cap_first}Repository.buildPrimaryKey();
        Assert.assertNotNull(primaryKey);</#if>
    }</#if>

    <#if !(api.settings??) || api.settings.enableCreate!true>@Test
    public void testCreate${api.name?cap_first}() throws Exception {<#if multiPrimaryKey><#list nonAutoPrimaryFields as p>
        ${p.type} ${p.name} = null;</#list>
        ${api.name?cap_first}PK id = ${api.name?cap_first}Repository.buildPrimaryKey(<#list nonAutoPrimaryFields as p>${p.name}<#if p_has_next>, </#if></#list>);<#elseif primaryKey?? && primaryKey.config.createOrUpdate.enabled>
        ${primaryKey.type} ${primaryKey.name} = null;</#if>
        ${api.name?cap_first}UpdateBean ${api.name?uncap_first}UpdateBean = ${api.name?cap_first}UpdateBean.builder()<#list normalFields as p><#if p.config?? && p.config.createOrUpdate?? && p.config.createOrUpdate.enabled>
                .${p.name}(<@toSetFieldValue p/>)</#if></#list>
                .build();
        ${entityName} ${api.name?uncap_first} = repository.create${api.name?cap_first}(database, <#if multiPrimaryKey>id, <#elseif primaryKey?? && primaryKey.config.createOrUpdate.enabled>${primaryKey.name}, </#if>${api.name?uncap_first}UpdateBean);
        Assert.assertNotNull(${api.name?uncap_first});
    }

    @Test
    public void testUpdate${api.name?cap_first}() throws Exception {<#if multiPrimaryKey><#list nonAutoPrimaryFields as p>
        ${p.type} ${p.name} = null;</#list>
        ${api.name?cap_first}PK id = ${api.name?cap_first}Repository.buildPrimaryKey(<#list nonAutoPrimaryFields as p>${p.name}<#if p_has_next>, </#if></#list>);
        Assert.assertNotNull(id);<#elseif primaryKey?? && !primaryKey.autoIncrement>
        ${primaryKey.type} id = null;
        Assert.assertNotNull(id);</#if>
        ${entityName} ${api.name?uncap_first} = ${entityName}.builder(database)<@toSetIdValue/>.build().load();
        if (${api.name?uncap_first} != null) {
            ${api.name?cap_first}UpdateBean ${api.name?uncap_first}UpdateBean = ${api.name?cap_first}UpdateBean.builder()<#list normalFields as p><#if p.config?? && p.config.createOrUpdate?? && p.config.createOrUpdate.enabled>
                    .${p.name}(<@toSetFieldValue p/>)</#if></#list>
                    .build();
            ${api.name?uncap_first} = repository.update${api.name?cap_first}(database, id, ${api.name?uncap_first}UpdateBean<#if lastModifyTimeProp?? && !lastModifyTimeProp.foreign>, ${api.name?uncap_first}.get${lastModifyTimeProp.name?cap_first}()</#if>);
        }
        Assert.assertNotNull(${api.name?uncap_first});
    }

    @Test
    public void testUpdate${api.name?cap_first}s() throws Exception {<#if multiPrimaryKey><#list nonAutoPrimaryFields as p>
        ${p.type} ${p.name} = null;</#list>
        ${api.name?cap_first}PK id = ${api.name?cap_first}Repository.buildPrimaryKey(<#list nonAutoPrimaryFields as p>${p.name}<#if p_has_next>, </#if></#list>);
        Assert.assertNotNull(id);<#elseif primaryKey?? && !primaryKey.autoIncrement>
        ${primaryKey.type} id = null;
        Assert.assertNotNull(id);</#if>
        Fields fields = Fields.create();
        Params values = Params.create();
        Assert.assertTrue(fields.isEmpty());
        Assert.assertTrue(values.isEmpty());
        int effectCounts = repository.update${api.name?cap_first}s(database, ArrayUtils.toArray(id), fields, values);
        Assert.assertTrue(effectCounts > 0);
    }</#if>

    <#if !(api.settings??) || api.settings.enableQuery!true><#if !api.view>@Test
    public void testQuery${api.name?cap_first}() throws Exception {<#if multiPrimaryKey><#list nonAutoPrimaryFields as p>
        ${p.type} ${p.name} = null;</#list>
        ${api.name?cap_first}PK id = ${api.name?cap_first}Repository.buildPrimaryKey(<#list nonAutoPrimaryFields as p>${p.name}<#if p_has_next>, </#if></#list>);
        Assert.assertNotNull(id);<#elseif primaryKey?? && !primaryKey.autoIncrement>
        ${primaryKey.type} id = null;
        Assert.assertNotNull(id);</#if>
        Fields excludedFields = Fields.create();
        ${api.name?cap_first}VO ${api.name?uncap_first} = repository.query${api.name?cap_first}(database, id, excludedFields);
        Assert.assertNotNull(${api.name?uncap_first});
    }</#if>

    @Test
    public void testQuery${api.name?cap_first}s() throws Exception {
        ${api.name?cap_first}Bean ${api.name?uncap_first}Bean = ${api.name?cap_first}Bean.builder()<#list normalFields as p><#if p.config?? && p.config.query?? && p.config.query.enabled><#if p.config.query.validation?? && p.config.query.validation.dateTime?? && p.config.query.validation.dateTime.enabled>
            .start${p.name?cap_first}(0L)
            .end${p.name?cap_first}(0L)<#else>
            .${p.name}(<@toSetFieldValue p/>)</#if></#if></#list>
            .build();
        Cond otherCond = null;
        OrderBy orderBy = null;
        Fields excludedFields = Fields.create();
        IResultSet<${api.name?cap_first}VO> ${api.name?uncap_first}s = repository.query${api.name?cap_first}s(database, ${api.name?uncap_first}Bean, otherCond, orderBy, excludedFields, Page.create(1));
        Assert.assertNotNull(${api.name?uncap_first}s);
    }</#if>

    <#if !api.view><#if !(api.settings??) || api.settings.enableRemove!true>@Test
    public void testRemove${api.name?cap_first}() throws Exception {<#if multiPrimaryKey><#list nonAutoPrimaryFields as p>
        ${p.type} ${p.name} = null;</#list>
        ${api.name?cap_first}PK id = ${api.name?cap_first}Repository.buildPrimaryKey(<#list nonAutoPrimaryFields as p>${p.name}<#if p_has_next>, </#if></#list>);
        Assert.assertNotNull(id);<#elseif primaryKey?? && !primaryKey.autoIncrement>
        ${primaryKey.type} id = null;
        Assert.assertNotNull(id);</#if>
        int effectCounts = repository.remove${api.name?cap_first}(database, id);
        Assert.assertTrue(effectCounts > 0);
    }

    @Test
    public void testRemove${api.name?cap_first}s() throws Exception {<#if multiPrimaryKey><#list nonAutoPrimaryFields as p>
        ${p.type} ${p.name} = null;</#list>
        ${api.name?cap_first}PK id = ${api.name?cap_first}Repository.buildPrimaryKey(<#list nonAutoPrimaryFields as p>${p.name}<#if p_has_next>, </#if></#list>);
        Assert.assertNotNull(id);<#elseif primaryKey?? && !primaryKey.autoIncrement>
        ${primaryKey.type} id = null;
        Assert.assertNotNull(id);</#if>
        int effectCounts = repository.remove${api.name?cap_first}s(database, ArrayUtils.toArray(id));
        Assert.assertTrue(effectCounts > 0);
    }</#if></#if>
}